package com.example.wxai.controller;



import com.example.wxai.pijo.dto.EssatDto;
import com.example.wxai.pijo.entity.Essay;
import com.example.wxai.pijo.entity.User;
import com.example.wxai.pijo.vo.EssayVo;
import com.example.wxai.pijo.vo.HandUpVo;
import com.example.wxai.pijo.vo.IndexVo;
import com.example.wxai.result.PageResult;
import com.example.wxai.result.Result;
import com.example.wxai.service.IEssayService;
import com.example.wxai.service.IUserService;
import com.example.wxai.utils.AliOSSUtlis;
import com.example.wxai.utils.DatetimeUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Slf4j
@RestController
@RequestMapping("/essay")
public class EssayController {
    @Resource
    private AliOSSUtlis aliOSSUtlis;
    @Resource
    private IEssayService essayService;

    @Resource
    private IUserService userService;

    @PostMapping("/add")
    public Result<String> add(@RequestBody EssatDto essatDto) {
        log.info("上传文章{}",essatDto);
        //新文章
        Essay essay=new Essay();
        BeanUtils.copyProperties(essatDto,essay);
        essay.setEssayLikes(0);
        essay.setEssayReviews(0);
        essay.setEssayTime(DatetimeUtil.getTheTimeInMinutes());
        essayService.save(essay);
        //用户文字数量加1
        userService.addEssayCount(essatDto.getEssayAuthorId());
        return Result.success();
    }


    @PostMapping("/uploadImg")
    public Result<String> uploadImg(MultipartFile essayImgs) throws Exception {
        log.info("上传图片");
        String upload = aliOSSUtlis.upload(essayImgs);
        return Result.success(upload);
    }


    @GetMapping("/getEssay")
    public Result<EssayVo> getEssayById(String essayId){
        log.info("查看文章详情{}",essayId);
        Essay byId = essayService.getById(essayId);
        String[] imgs = byId.getEssayImgs().split(",");
        log.info("文章图片{}",imgs);
        EssayVo essayVo = new EssayVo();
        BeanUtils.copyProperties(byId,essayVo);
        essayVo.setEssayImgs(imgs);
        //查询用户id和头像
        User byId1 = userService.lambdaQuery().eq(User::getUserId,byId.getEssayAuthorId()).one();
        essayVo.setEssayAuthorName(byId1.getUserName());
        essayVo.setEssayAuthorImg(byId1.getUserImg());
        return Result.success(essayVo);
    }

    //分页热点查询
    @GetMapping("/getEssayList")
    public PageResult<HandUpVo> getEssayList(Integer page){
        log.info("分页查询");
        PageResult<HandUpVo> pageResult=essayService.queryPage(page);
        return pageResult;
    }


    //分页首页查询
    @GetMapping("getEssayIndex")
    public PageResult<IndexVo> getIndexPage(Integer page, String key){
        log.info("首页分页查询");
        PageResult<IndexVo> pageResult=essayService.queryIndexPage(page,key);
        return pageResult;
    }





}
